package com.gel.shortLink.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.zookeeper.Op;

import java.util.Date;
import java.util.Optional;

import static com.gel.shortLink.common.constant.ShortLinkConstant.DEFAULT_CACHE_VALID_TIME;

public class LinkUtil {
    public static long getLinkCacheValidTime(Date validDate){
      return   Optional.ofNullable(validDate)
                .map(each-> DateUtil.between(new Date(),each, DateUnit.MS))
                .orElse(DEFAULT_CACHE_VALID_TIME);
    }
    public static String getBrowserInfo(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return "Unknown";
        }

        userAgent = userAgent.toLowerCase();

        if (userAgent.contains("msie") || userAgent.contains("trident")) {
            return "Internet Explorer";
        } else if (userAgent.contains("edge")) {
            return "Microsoft Edge";
        } else if (userAgent.contains("chrome")) {
            return "Google Chrome";
        } else if (userAgent.contains("firefox")) {
            return "Mozilla Firefox";
        } else if (userAgent.contains("safari")) {
            return "Apple Safari";
        } else if (userAgent.contains("opera")) {
            return "Opera";
        } else {
            return "Unknown";
        }
    }

    public static String getOsInfo(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return "Unknown";
        }

        userAgent = userAgent.toLowerCase();

        if (userAgent.contains("windows")) {
            return "Windows";
        } else if (userAgent.contains("mac")) {
            return "Mac OS";
        } else if (userAgent.contains("linux")) {
            return "Linux";
        } else if (userAgent.contains("android")) {
            return "Android";
        } else if (userAgent.contains("iphone") || userAgent.contains("ipad")) {
            return "iOS";
        } else {
            return "Unknown";
        }
    }
    /**
     * 获取设备类型
     * @param request HttpServletRequest对象
     * @return 设备类型 (Mobile, Tablet, Desktop, Bot, Unknown)
     */
    public static String getDeviceType(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent == null) {
            return "Unknown";
        }

        userAgent = userAgent.toLowerCase();

        // 检测爬虫/Bot
        if (userAgent.contains("bot") || userAgent.contains("spider") ||
                userAgent.contains("crawler") || userAgent.contains("googlebot")) {
            return "Bot";
        }

        // 检测移动设备
        boolean isMobile = userAgent.contains("mobile") ||
                userAgent.contains("android") ||
                userAgent.contains("iphone") ||
                userAgent.contains("ipod") ||
                userAgent.contains("windows phone");

        // 检测平板设备
        boolean isTablet = userAgent.contains("ipad") ||
                (userAgent.contains("android") && !userAgent.contains("mobile")) ||
                userAgent.contains("tablet");

        if (isMobile && !isTablet) {
            return "Mobile";
        } else if (isTablet) {
            return "Tablet";
        } else {
            return "Desktop";
        }
    }

    /**
     * 检测网络类型 (基于HTTP请求头)
     * @param request HttpServletRequest对象
     * @return 网络类型 (Mobile, WiFi, Unknown)
     */
    public static String getNetworkType(HttpServletRequest request) {
        // 通过特定Header判断移动网络
        String via = request.getHeader("Via");
        String xForwardedFor = request.getHeader("X-Forwarded-For");
        String xWapProfile = request.getHeader("X-Wap-Profile");
        String profile = request.getHeader("Profile");

        if (via != null && via.toLowerCase().contains("wap")) {
            return "Mobile";
        }
        if (xForwardedFor != null && xForwardedFor.matches(".*(3g|4g|5g).*")) {
            return "Mobile";
        }
        if (xWapProfile != null || profile != null) {
            return "Mobile";
        }

        // 通过User-Agent判断
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null && userAgent.toLowerCase().contains("mobile")) {
            return "Mobile";
        }

        // 默认认为是WiFi/有线网络
        return "WiFi";
    }
}
